package cn.org.timespace.ons.util.json;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

public class JSONResponseHepler
{
    public static void writeSuccessResponse(HttpServletResponse response)
    {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out;
        try
        {
            out = response.getWriter();
            JSONObject result = new JSONObject();
            result.put("flag", "1");
            out.print(result.toString());
            out.flush();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
       
    }
    
    public static void writeFailureResponse(HttpServletResponse response)
    {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out;
        try
        {
            out = response.getWriter();
            JSONObject result = new JSONObject();
            result.put("flag", "0");
            out.print(result.toString());
            out.flush();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    
    public static void writeJson(HttpServletResponse response, JSONObject result)
    {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out;
        try
        {
            out = response.getWriter();
            
            out.print(result.toString());
            out.flush();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
    
    public static void writeJson(HttpServletResponse response, JSONArray result)
    {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out;
        try
        {
            out = response.getWriter();
            
            out.print(result.toString());
            out.flush();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
    //库存不足
    public static void writeFailureOutResponse(HttpServletResponse response)
    {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out;
        try
        {
            out = response.getWriter();
            JSONObject result = new JSONObject();
            result.put("flag", "3");
            out.print(result.toString());
            out.flush();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    
}
